package com.drinks.yiniang.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.drinks.yiniang.App;
import com.drinks.yiniang.common.help.UserTokenSingleton;
import com.hansen.library.Constants;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/21 0021.
 */
public class SharedPreUtils {

    private static SharedPreUtils sharedPreferencesUtil;
    private SharedPreferences sharedPreferences;
    private static int mode = Context.MODE_PRIVATE;
    private SharedPreferences.Editor editor;
    private static String sharedPreferenceName = Constants.SHARED_PREFERENCE_NAME;


    public static SharedPreUtils getInstance() {
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = new SharedPreUtils();
        }
        return sharedPreferencesUtil;
    }

    public static SharedPreUtils getInstance(String sharedPreferenceName, int mode) {
        SharedPreUtils.sharedPreferenceName = sharedPreferenceName;
        SharedPreUtils.mode = mode;
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = new SharedPreUtils();
        }
        return sharedPreferencesUtil;
    }

    public SharedPreferences getSharedPreferences() {
        if (sharedPreferences == null) {
            sharedPreferences = App.getInstance().getSharedPreferences(sharedPreferenceName, mode);
        }
        return sharedPreferences;
    }

    //保存token
    public void saveUserToken(String token) {
        putString(Constants.SP_UserToken, token);
    }

    public String getUserToken() {
        return getString(Constants.SP_UserToken);
    }

    //保存id
    public void saveUserID(String id) {
        putString(Constants.SP_UserID, id);
    }

    public String getUserID() {
        return getString(Constants.SP_UserID);
    }

    //保存角色
    public void saveUserRole(String role) {
        putString(Constants.SP_UserRole, role);
    }

    public String getUserRole() {
        return getString(Constants.SP_UserRole);
    }

    //保存昵称
    public void saveUserName(String name) {
        putString(Constants.SP_UserName, name);
    }

    public String getUserName() {
        return getString(Constants.SP_UserName);
    }

    //保存头像
    public void saveUserAvatar(String avatar) {
        putString(Constants.SP_UserAvatar, avatar);
    }

    public String getUserAvatar() {
        return getString(Constants.SP_UserAvatar);
    }

    //保存id
    public void saveIsOpenPayPasswordID(String isOpen) {
        putString(Constants.SP_IsOpenPayPassword, isOpen);
    }

    public String getIsOpenPayPassword() {
        return getString(Constants.SP_IsOpenPayPassword);
    }

    //保存app引导的版本
    public void saveAppGuidedVsersion(String guideVersion) {
        putString(Constants.SP_AppGuide, guideVersion);
    }

    public boolean isAppGuided() {
        return Constants.APP_GUIDE_VERSION.equals(getString(Constants.SP_AppGuide));
    }

    //保存极光推送id
    public void saveJPushID(boolean hasPushID) {
        putBoolean(Constants.SP_JPush, hasPushID);
    }

    public boolean isHasJPushID() {
        return getBoolean(Constants.SP_JPush, false);
    }

    public void clearLoginSharedPreference() {
        saveUserToken(null);
        saveUserRole(null);
        saveUserAvatar(null);
        saveUserID(null);
        saveUserName(null);
        saveIsOpenPayPasswordID(null);
        saveJPushID(false);
        //清除单类
        UserTokenSingleton.getInstance().setGlobalUserToken(null);
    }

    /**
     * 获取 SharedPreferences boolean值
     *
     * @param key 键
     * @return
     */
    public boolean getBoolean(String key) {
        return getSharedPreferences().getBoolean(key, false);
    }

    /**
     * 获取 SharedPreferences boolean值
     *
     * @param key      键
     * @param defValue 默认值
     * @return
     */
    public boolean getBoolean(String key, boolean defValue) {
        return getSharedPreferences().getBoolean(key, defValue);
    }

    /**
     * 获取 SharedPreferences String值
     *
     * @param key      键
     * @param defValue 默认值
     * @return
     */
    public String getString(String key, String defValue) {
        return getSharedPreferences().getString(key, defValue);
    }

    /**
     * 获取 SharedPreferences String值
     *
     * @param key 键
     * @return
     */
    public String getString(String key) {
        return getString(key, "");
    }

    public void putString(String key, String value) {
        editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void putInt(String key, int value) {
        editor = getSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取 SharedPreferences String值
     *
     * @param key      键
     * @param defValue 默认值
     * @return
     */
    public Integer getInt(String key, int defValue) {
        return getSharedPreferences().getInt(key, defValue);
    }

    public void putLong(String key, long value) {
        editor = getSharedPreferences().edit();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 获取 SharedPreferences String值
     *
     * @param key      键
     * @param defValue 默认值
     * @return
     */
    public long getLong(String key, long defValue) {
        return getSharedPreferences().getLong(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void removeKey(String key) {
        editor = getSharedPreferences().edit();
        editor.remove(key);
        editor.commit();
    }

    public void putMap( String key, Map<String, Object> maps) {
        if(maps == null) {
            putString(key, null);

            return;
        }

        putString(key, JSON.toJSONString(maps));
    }

    public Map<String, Object> getMap(String key) {

        String result = getString(key, "");

//        try {
//            return JSONObject.parseObject(result, new TypeReference<Map<String, Object>>(){});
//        } catch (JSONException e) {
//
//        }

        return null;
    }

    public void saveListMap(String key, List<Map<String, String>> datas) {
        JSONArray mJsonArray = new JSONArray();

        for (int i = 0; i < datas.size(); i++) {
            Map<String, String> itemMap = datas.get(i);
            Iterator<Map.Entry<String, String>> iterator = itemMap.entrySet().iterator();

            JSONObject object = new JSONObject();

            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                try {
                    object.put(entry.getKey(), entry.getValue());
                } catch (JSONException e) {

                }
            }
            mJsonArray.add(object);
        }

        putString(key, mJsonArray.toString());
    }

    public List<Map<String, String>> getListMap(String key) {
        List<Map<String, String>> datas = new ArrayList<Map<String, String>>();

        String result = getString(key, "");

        try {
            JSONArray array = new JSONArray();
            array.add(result);

            Iterator<Object> iterator = array.iterator();

            while (iterator.hasNext()) {
                Map<String, String> itemMap = (Map<String, String>) iterator.next();

                if (itemMap != null) {
                    for (int j = 0; j < itemMap.size(); j++) {
                        String name = itemMap.get(j);
                        String value = itemMap.get(name);
                        itemMap.put(name, value);
                    }
                }
                datas.add(itemMap);
            }
        } catch (JSONException e) {

        }

        return datas;
    }
}
